package com.test.daily.leetcode.y2021.m10.day1008;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @descriptions: findRepeatedDnaSequences
 * @author: Tom
 * @date: 2021/10/8 上午 09:40
 * @version: 1.0
 */
public class Code01_FindRepeatedDnaSequences {
    public static void main(String[] args) {
        String s = "AAAA";
        List<String> alist = new ArrayList<>();
        alist = findRepeatedDnaSequences(s);
        alist.forEach(
                ele -> {
                    System.out.println(ele);
                }
        );

    }

    public static List<String> findRepeatedDnaSequences(String s) {
        ArrayList<String> arrayList = new ArrayList<>();
        if (s == null || s.length() <= 10) {
            return arrayList;
        }
        HashMap<String, Integer> hashMap = new HashMap<>();
        for (int i = 0; i <= s.length() - 10; i++) {
            String splitStr = s.substring(i, i + 10);
            if (hashMap.get(splitStr) == null) {
                hashMap.put(splitStr, 1);
            } else {
                hashMap.put(splitStr, hashMap.get(splitStr) + 1);
            }
        }

        hashMap.forEach(
                (key, value) -> {
                    if (value > 1) {
                        arrayList.add(key);
                    }
                }
        );
        return arrayList;
    }
}
